// any 和 unknown 区别
// any 可以赋值给任意类型 会关闭ts的类型检查
// unknown 可以是任何类型只能赋值给unknown和any 类型，不能赋值给其他类型。
let v: any;
v = 1;
v = "123";
let v1: string = v;

let bb: object
bb = null 

let v2: unknown;
v2 = 1;
v2 = "123";
let v3: string = v2; // error

let v4: unknown = { foo: 123 };
v4.foo;
v4 + 12;

let ccc: likun

let v5: never;
function fn(): never {
  throw new Error("error");
}

let v6: number = fn();

v5 = 2;

let obj: Object;
obj = 123;

let obj1: object;
obj1 = 123;

let a1: number = 23;
a1 = undefined;
a1 = null;

let age: number = 24;

age = null; // 正确
age = undefined; // 正确

let b: undefined = null;

obj = null;
obj1 = null;

function test(str: string): string;
function test(str: any[]): string;
function test(stringOrArray: string | any[]): string | any[] {
  if (typeof stringOrArray === "string")
    return stringOrArray.split("").reverse().join("");
  else return stringOrArray.slice().reverse();
}

test([12, 342]);

interface Point {
  x: number;
  y: number;
}

// 正确
type PointCopy1 = {
  [Key in keyof Point]: Point[Key];
};

// 报错
interface PointCopy2 {
  [Key in keyof Point]: Point[Key];
};

interface Todo {

  title: string;
  description: string;
  completed: boolean
}
type MyPick<T, K extends keyof T> = {
  [key in K]: T[key]
}
type TodoPreview = MyPick<Todo, "title" | "completed">;
const todo: TodoPreview = {
  title: 'likun',
  completed: false,
}

// 
interface userInfo {
  name: string
  age: number
}
// 对键名遍历
type keyofValue = keyof userInfo
const a: keyofValue = 'name'

type name = 'firstname' | 'lastname'
type TName = {
  [key in name]: string
}

function getValue<T extends Object, K extends keyof T>(o: T, key: K) {
  return o[key]
}
let obj11 = {
  name: 'aaaa',
  age: 'dfadfasd'
}
getValue(obj11, 'name')

async function fetchData(): Promise<any> {
  const response = await fetch('https://api.example.com');
  const data = await response.json();
  return data;
  }
  fetchData().then(data => {
  console.log(data);
  });